package com.isdepci.project.carbon.service.impl;

import com.isdepci.framework.web.domain.AjaxResult;
import com.isdepci.project.carbon.domain.BDirectRelease;
import com.isdepci.project.carbon.domain.param.WeatherDataExcel;
import com.isdepci.project.carbon.mapper.BDirectReleaseMapper;
import com.isdepci.project.carbon.service.IBDirectReleaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 * 直接释放Service业务层处理
 *
 * @author isdepci
 * @date 2025-03-29
 */
@Service
public class BDirectReleaseServiceImpl implements IBDirectReleaseService
{
    @Autowired
    private BDirectReleaseMapper bDirectReleaseMapper;


    @Override
    public AjaxResult calDirectRelease(Long projectId) {
        // 查询出所有的BDirectRelease数据
        List<BDirectRelease> bDirectReleases = bDirectReleaseMapper.selectBDirectReleaseByProjectId(projectId);
        // 初始化总的直接释放量
        BigDecimal totalDirectRelease = BigDecimal.ZERO;
        // 遍历每一条数据，计算直接释放量
        for (BDirectRelease bDirectRelease : bDirectReleases) {
            // 计算Q消耗 (weightBefore - weightAfter) * totalUsage
            BigDecimal qConsumption = bDirectRelease.getWeightBefore()
                    .subtract(bDirectRelease.getWeightAfter())
                    .multiply(new BigDecimal(bDirectRelease.getTotalUsage()));
            // 计算E直接释放 Q消耗 * purity * emissionFactor
            BigDecimal directRelease = qConsumption
                    .multiply(bDirectRelease.getPurity().divide(BigDecimal.valueOf(100)))
                    .multiply(bDirectRelease.getEmissionFactor());
            bDirectRelease.setCarbonEmissions(directRelease);
            bDirectReleaseMapper.updateBDirectRelease(bDirectRelease);
            // 累加直接释放量
            totalDirectRelease = totalDirectRelease.add(directRelease);
        }
        // 返回总的直接释放量
        return AjaxResult.success(totalDirectRelease.setScale(2, RoundingMode.HALF_UP));
    }


    public AjaxResult changeDataByProjectId(WeatherDataExcel excel) {
        List<BDirectRelease> list =  excel.getDirectReleaseList();
        list.forEach(item->{
            if(excel.getProjectId()!=null){
                item.setProjectId(excel.getProjectId());
            }
        });
        if(list.size()>0){
            bDirectReleaseMapper.insertList(list);
        }
        return AjaxResult.success();
    }



    /**
     * 查询直接释放
     */
    @Override
    public BDirectRelease selectBDirectReleaseById(Long id)
    {
        return bDirectReleaseMapper.selectBDirectReleaseById(id);
    }

    /**
     * 查询直接释放列表
     */
    @Override
    public List<BDirectRelease> selectBDirectReleaseList(BDirectRelease bDirectRelease)
    {
        return bDirectReleaseMapper.selectBDirectReleaseList(bDirectRelease);
    }

    /**
     * 新增直接释放
     */
    @Override
    public int insertBDirectRelease(BDirectRelease bDirectRelease)
    {
        return bDirectReleaseMapper.insertBDirectRelease(bDirectRelease);
    }

    /**
     * 修改直接释放
     */
    @Override
    public int updateBDirectRelease(BDirectRelease bDirectRelease)
    {
        return bDirectReleaseMapper.updateBDirectRelease(bDirectRelease);
    }

    /**
     * 批量删除直接释放
     */
    @Override
    public int deleteBDirectReleaseByIds(Long[] ids)
    {
        return bDirectReleaseMapper.deleteBDirectReleaseByIds(ids);
    }

    /**
     * 删除直接释放信息
     */
    @Override
    public int deleteBDirectReleaseById(Long id)
    {
        return bDirectReleaseMapper.deleteBDirectReleaseById(id);
    }


}
